package com.xb.controller;

import com.xb.entity.Department;
import com.xb.service.IDepartmentService;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Controller
 * /api/department
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    /**
     * 条件查询
     */
    @GetMapping("/getDepartment")
    @RequiresPermissions("system:department:find")
    public Object find() {
        Result result = new Result();
        result.setData(departmentService.selectDepartmentList(null));
        return result;
    }

    @RequestMapping("/page")
    @RequiresPermissions("system:department:page")
    public Object findPage(Department obj, @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        return departmentService.selectDepartmentList(obj, page, limit);
    }

    @DeleteMapping("/{id}")
    @RequiresPermissions("system:department:del")
    public Object del(@PathVariable Integer id) {
        return departmentService.deleteDepartmentById(id);
    }

    /**
     * 添加
     */
    @PostMapping
    @RequiresPermissions("system:department:add")
    public Object add(@RequestBody Department obj) {
        return departmentService.add(obj);
    }

    /**
     * 修改
     */
    @PutMapping
    @RequiresPermissions("system:department:update")
    public Object update(@RequestBody Department obj) {
        Result result = new Result();
        if(departmentService.updateDepartment(obj)>0){
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("修改成功！");
        }else{
            result.setMessage("修改失败！");
        }
        return result;
    }


}
